/*
版权@hehung所有，转载请申明
email：1398660197@qq.com
*/
#include "app_key.h"
#include "app_main.h"
#include <drv/pin.h>
#include <drv/gpio_pin.h>
#include "app_display.h"
#include "app_player.h"


#define KEY_READ_KEY1                       (csi_gpio_pin_read(&key1))
#define KEY_READ_KEY2                       (csi_gpio_pin_read(&key2))


static csi_gpio_pin_t key1;
static csi_gpio_pin_t key2;
static aos_task_t key_task_h;    /* Key task handle */
static uint16_t key_task_cycle_ms = KEY_TASK_CYCLE_MS;    /* ms */
static csi_gpio_pin_state_t key_status[KEY_ALL_NUM] = {GPIO_PIN_HIGH, GPIO_PIN_HIGH};
static csi_gpio_pin_state_t last_key_status[KEY_ALL_NUM] = {GPIO_PIN_HIGH, GPIO_PIN_HIGH};


static void Key_Pinmuxinit(void);
static void Key_TaskEntry(void *arg);
static void Key_Manager(void);
static void Key_ShortPress(uint8_t key);
static void Key_LongPress(uint8_t key);
static void Key_PressHolding(uint8_t key);


static void Key_Pinmuxinit(void)
{
	csi_pin_set_mux(PA11, PIN_FUNC_GPIO);
    csi_pin_set_mux(PA12, PIN_FUNC_GPIO);
	
    csi_gpio_pin_init(&key1, PA11);
    csi_gpio_pin_dir(&key1, GPIO_DIRECTION_INPUT);
    csi_gpio_pin_init(&key2, PA12);
    csi_gpio_pin_dir(&key2, GPIO_DIRECTION_INPUT);
	
	/* 使能debounce模式 */
	csi_gpio_pin_debounce(&key1, 1);
	csi_gpio_pin_debounce(&key2, 1);
}

/* 按键函数初始化 */
void Key_TaskInit(void)
{
	int task_s;
	
	Key_Pinmuxinit();
	 
	task_s = aos_task_new_ext(&key_task_h,
								"key_task", 
								Key_TaskEntry, 
								NULL, 
								1024, 
								2);
	if (0 == task_s)
	{
		printf("APP_KEY Task create success\r\n");
	}
	else
	{
		printf("APP_KEY Task create failed\r\n");
	}
}

/* 按键函数任务入口 */
static void Key_TaskEntry(void *arg)
{
    while (1) 
	{
		Key_Manager();
		
		aos_msleep(key_task_cycle_ms);
    }
}

static void Key_Manager(void)
{
	uint8_t i;
	static uint16_t key_press_cnt[KEY_ALL_NUM] = {0, 0};
	
	/* 获取当前按键按下的状态 */
	key_status[KEY_NUM1] =  KEY_READ_KEY1;
	key_status[KEY_NUM2] =  KEY_READ_KEY2;
	
	for (i=0; i<KEY_ALL_NUM; i++)
	{
		/* 按键释放 */
		if ((key_status[i] == GPIO_PIN_HIGH) && (last_key_status[i] == GPIO_PIN_LOW))
		{
			if(key_press_cnt[i] < KEY_SHORT_PRESS_CNT)
			{
				Key_ShortPress(i);
			}
			else
			{
				Key_LongPress(i);
			}
			
			key_press_cnt[i] = 0;
		}
		/* 按键按下 */
		else if((key_status[i] == GPIO_PIN_LOW) && (last_key_status[i] == GPIO_PIN_HIGH))
		{
			/* Do Nothing */
		}
		/* 按键保持释放 */
		else if((key_status[i] == GPIO_PIN_HIGH) && (last_key_status[i] == GPIO_PIN_HIGH))
		{
			/* Do Nothing */
		}
		/* 按键保持按下 */
		else if((key_status[i] == GPIO_PIN_LOW) && (last_key_status[i] == GPIO_PIN_LOW))
		{
			if(key_press_cnt[i] >= KEY_SHORT_PRESS_CNT)
			{
				Key_PressHolding(i);
				key_press_cnt[i] = KEY_SHORT_PRESS_CNT;
			}
			else
			{
				key_press_cnt[i] ++;
			}
		}
	}
	
	/* 记录上一个周期按键按下的状态 */
	last_key_status[KEY_NUM1] = key_status[KEY_NUM1];
	last_key_status[KEY_NUM2] = key_status[KEY_NUM2];
}

static void Key_ShortPress(uint8_t key)
{
	/* 播放按键音效 */
	Player_SetPressButtonFlg();

	/* 暂停闹钟 */
	if (ALARM_WARNING_ON == Alarm_GetalarmFlag())
	{
		Alarm_StopWarning();
	}
	else
	{
		if (KEY_NUM1 == key)
		{
			Disp_Key1ShortPressJudge();
		}
		else
		{
			Disp_Key2ShortPressJudge();
		}
	}
}

static void Key_LongPress(uint8_t key)
{
		/* 播放按键音效 */
	Player_SetPressButtonFlg();

	/* 暂停闹钟 */
	if (ALARM_WARNING_ON == Alarm_GetalarmFlag())
	{
		Alarm_StopWarning();
	}
	else
	{
		if (KEY_NUM1 == key)
		{
			Disp_Key1LongPressJudge();
		}
		else
		{
			Disp_Key2LongPressJudge();
		}
	}
}

static void Key_PressHolding(uint8_t key)
{
	if (KEY_NUM1 == key)
	{
		Disp_Key1Holding();
	}
	else
	{
		Disp_Key2Holding();
	}
}